package com.haohan.jingyuan.customer.controller;

import cn.hutool.json.JSONUtil;
import com.haohan.jingyuan.customer.dto.req.CreateCustomerRequest;
import com.haohan.jingyuan.customer.dto.req.DeleteCustomerRequest;
import com.haohan.jingyuan.customer.dto.req.QueryCustomerListRequest;
import com.haohan.jingyuan.customer.dto.req.UpdateCustomerRequest;
import com.haohan.jingyuan.customer.dto.resp.CustomerInfoResp;
import com.haohan.jingyuan.customer.dto.resp.CustomerListResp;
import com.haohan.jingyuan.customer.service.CustomerService;
import com.haohan.jingyuan.gateway.aspect.NeedLogin;
import com.haohan.jingyuan.gateway.dto.GwRequest;
import com.haohan.jingyuan.gateway.dto.GwResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * 客户控制器
 *
 * @ClassName CustomerController
 * @Description 客户相关接口控制器
 * @Author hexijun
 * @Time 2025/01/19 10:00
 * @Version 1.0
 */
@Slf4j
@RestController
@RequiredArgsConstructor
public class CustomerController {

    /**
     * 客户服务
     */
    private final CustomerService customerService;

    /**
     * 创建客户
     *
     * @param request 创建客户请求
     * @return 创建结果
     */
    @PostMapping("/customer/create")
    @NeedLogin
    public GwResponse<CustomerInfoResp> createCustomer(@Valid @RequestBody GwRequest<CreateCustomerRequest> request) {
        log.info("CustomerController.createCustomer BEGIN ==> {}", JSONUtil.toJsonStr(request));

        CustomerInfoResp customerInfo = customerService.createCustomer(request.getRequestData());
        GwResponse<CustomerInfoResp> response = GwResponse.success("创建客户成功", customerInfo);

        log.info("CustomerController.createCustomer END ==> {}", JSONUtil.toJsonStr(response));
        return response;
    }

    /**
     * 更新客户
     *
     * @param request 更新客户请求
     * @return 更新结果
     */
    @PostMapping("/customer/update")
    @NeedLogin
    public GwResponse<CustomerInfoResp> updateCustomer(@Valid @RequestBody GwRequest<UpdateCustomerRequest> request) {
        log.info("CustomerController.updateCustomer BEGIN ==> {}", JSONUtil.toJsonStr(request));

        CustomerInfoResp customerInfo = customerService.updateCustomer(request.getRequestData());
        GwResponse<CustomerInfoResp> response = GwResponse.success("更新客户成功", customerInfo);

        log.info("CustomerController.updateCustomer END ==> {}", JSONUtil.toJsonStr(response));
        return response;
    }

    /**
     * 删除客户
     *
     * @param request 删除客户请求
     * @return 删除结果
     */
    @PostMapping("/customer/delete")
    @NeedLogin
    public GwResponse<String> deleteCustomer(@Valid @RequestBody GwRequest<DeleteCustomerRequest> request) {
        log.info("CustomerController.deleteCustomer BEGIN ==> {}", JSONUtil.toJsonStr(request));

        boolean success = customerService.deleteCustomer(request.getRequestData());
        GwResponse<String> response = null;
        if (success) {
            response = GwResponse.success("删除客户成功", null);
        } else {
            response = GwResponse.error("删除客户失败");
        }

        log.info("CustomerController.deleteCustomer END ==> {}", JSONUtil.toJsonStr(response));
        return response;
    }

    /**
     * 查询客户列表
     *
     * @param request 查询客户列表请求
     * @return 客户列表
     */
    @PostMapping("/customer/list")
    @NeedLogin
    public GwResponse<CustomerListResp> queryCustomerList(@Valid @RequestBody GwRequest<QueryCustomerListRequest> request) {
        log.info("CustomerController.queryCustomerList BEGIN ==> {}", JSONUtil.toJsonStr(request));

        CustomerListResp customerList = customerService.queryCustomerList(request.getRequestData());
        GwResponse<CustomerListResp> response = GwResponse.success("查询客户列表成功", customerList);

        log.info("CustomerController.queryCustomerList END ==> {}", JSONUtil.toJsonStr(response));
        return response;
    }

}
